package com.to8to.autotask.server;

import java.io.ByteArrayInputStream;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.elasticsearch.common.collect.Maps;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.to8to.autotask.deduction.ForfeitDeductTask;
import com.to8to.autotask.resulthandler.FailHandler;
import com.to8to.autotask.util.Constant;
import com.to8to.autotask.util.Utils;
import com.to8to.crm.common.DefaultServerDeamon;
import com.to8to.es.util.LogUtil;
import com.to8to.kitt.esb.NodeChangeEvent;
import com.to8to.thrift.crm.autotask.AutoTaskService;

/**
 * AutoTaskServer.java
 * 作 者：atom.liu
 * 版 本：v2.0
 * 日 期：2014年11月24日
 * 描 述：自动任务中对外的服务
 */
public class AutoTaskServer extends DefaultServerDeamon
{
    private static final Logger logger = LoggerFactory.getLogger(AutoTaskServer.class);

    private static final String TASK_PROP = "task.properties";

    public static void main(String[] args)
    {
        try
        {
            String instance = "2";
            if (args.length > 0)
                instance = args[0];

            AutoTaskServer server = new AutoTaskServer();
            AutoTaskServiceImpl iface = new AutoTaskServiceImpl();
            server.start(iface, AutoTaskService.class, instance);
        }
        catch (Exception e)
        {
            logger.error("AutoTaskServer服务启动失败！{} \n {}", e, LogUtil.logStackTrace(e));
        }
    }

    @Override
    public void onPrivateConfigChange(Map<String, NodeChangeEvent> configMap)
    {
        if (Utils.isNullOrEmpty(configMap))
            return;

        String[] files = new String[] { TASK_PROP, serverFile };
        for (String file : files)
        {
            NodeChangeEvent event = configMap.get(file);
            if (event == null || !event.isChange())
            {
                if (event == null)
                    logger.error("Can't find " + file);
                continue;
            }

            if (TASK_PROP.equals(file))
            {
                byte[] data = event.ndata;
                try
                {
                    PropertiesConfiguration prop = new PropertiesConfiguration();
                    prop.load(new ByteArrayInputStream(data));
                    String[] phones = prop.getStringArray("ignore_repeat_phone");
                    String[] noticeEmails = prop.getStringArray("fail_notic_email");
                    Map<String, List<String>> forfeitMap = Maps.newHashMap();
                    forfeitMap.put("not_cost_uid", Arrays.asList(prop.getStringArray("not_cost_uid")));
                    forfeitMap.put("not_cost_yid", Arrays.asList(prop.getStringArray("not_cost_yid")));
                    forfeitMap.put("not_cost_city", Arrays.asList(prop.getStringArray("not_cost_city")));
                    forfeitMap.put("new_not_cost_city", Arrays.asList(prop.getStringArray("new_not_cost_city")));
                    forfeitMap.put("need_cost_uid", Arrays.asList(prop.getStringArray("need_cost_uid")));
                    Map<String, List<String>> channelMap = Maps.newHashMap();
                    channelMap.put("h5", Arrays.asList(prop.getStringArray("h5")));
                    channelMap.put("pc", Arrays.asList(prop.getStringArray("pc")));
                    FailHandler.initNoticeEmail(noticeEmails);
                    ForfeitDeductTask.initConfig(forfeitMap);
                    Constant.initIgnoreRepeatPhone(phones);
                    Constant.initChannelInfo(channelMap);
                    Constant.initSpecCityInfo(prop.getStringArray("spec_city"));
                }
                catch (ConfigurationException e)
                {
                    logger.error("AutoTaskServer.onPrivateConfigChange() 加载配置失败！{} \n {}", e, LogUtil.logStackTrace(e));
                }
            }
            else if (serverFile.equals(file))
            {
                super.onPrivateConfigChange(configMap);
            }
        }
    }
}
